package com.zyh.demo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;

/**
 * @Author zyh
 * @Date 2024/8/6 14:08
 * @Description 内存流的读和写
 */

public class IODemo4 {
    public static void main(String[] args) throws IOException {
        //写入内存
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(65);
        for (int i = 0; i < 10; i++) {
            bos.write("abc".getBytes());
        }
        byte[] bytes = bos.toByteArray();
        System.out.println(Arrays.toString(bytes));
        bos.flush();
        bos.close();
        //从内存中读取
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        int len=-1;
        byte[] car=new byte[1024];
        while ((len=bis.read(car))!=-1){
            PrintStream ps = new PrintStream(System.out,true,"UTF-8");
            ps.println(new String(car,0,len));
        }
        bis.close();
    }
}
